package com.it.labservice.config;

import com.it.labservice.entity.*;
import com.it.labservice.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;

@Component
public class DataInitializer implements CommandLineRunner {

    @Autowired
    private CollegeRepository collegeRepository;
    
    @Autowired
    private TermRepository termRepository;
    
    @Autowired
    private UserRepository userRepository;
    
    @Autowired
    private LaboratoryRepository laboratoryRepository;
    
    @Autowired
    private SafetyCheckRepository safetyCheckRepository;

    @Override
    public void run(String... args) throws Exception {
        // 检查是否已经有数据，如果有就不重复插入
        if (userRepository.count() > 0) {
            System.out.println("数据已存在，跳过初始化");
            return;
        }
        
        System.out.println("开始初始化数据...");
        
        // 创建学院
        College college1 = new College();
        college1.setName("计算机科学与技术学院");
        college1.setCreateTime(LocalDateTime.now());
        college1 = collegeRepository.save(college1);
        
        College college2 = new College();
        college2.setName("机械工程学院");
        college2.setCreateTime(LocalDateTime.now());
        college2 = collegeRepository.save(college2);
        
        College college3 = new College();
        college3.setName("化学化工学院");
        college3.setCreateTime(LocalDateTime.now());
        college3 = collegeRepository.save(college3);
        
        College college4 = new College();
        college4.setName("物理学院");
        college4.setCreateTime(LocalDateTime.now());
        college4 = collegeRepository.save(college4);
        
        College college5 = new College();
        college5.setName("生物学院");
        college5.setCreateTime(LocalDateTime.now());
        college5 = collegeRepository.save(college5);
        
        // 创建学期
        Term term1 = new Term();
        term1.setName("2023学年上学期");
        term1.setStartDate(LocalDate.of(2023, 2, 20));
        term1.setEndDate(LocalDate.of(2023, 7, 10));
        term1.setStatus(Term.TermStatus.ENDED);
        termRepository.save(term1);
        
        Term term2 = new Term();
        term2.setName("2023学年下学期");
        term2.setStartDate(LocalDate.of(2023, 8, 28));
        term2.setEndDate(LocalDate.of(2024, 1, 15));
        term2.setStatus(Term.TermStatus.CURRENT);
        termRepository.save(term2);
        
        Term term3 = new Term();
        term3.setName("2024学年上学期");
        term3.setStartDate(LocalDate.of(2024, 2, 26));
        term3.setEndDate(LocalDate.of(2024, 7, 15));
        term3.setStatus(Term.TermStatus.FUTURE);
        termRepository.save(term3);
        
        // 创建用户
        User admin = new User();
        admin.setUsername("admin");
        admin.setPassword("123456");
        admin.setName("超级管理员");
        admin.setRole(User.UserRole.ADMIN);
        admin.setContactInfo("admin@university.edu");
        admin.setIsActive(true);
        admin.setCreateTime(LocalDateTime.now());
        admin = userRepository.save(admin);
        
        User zhang = new User();
        zhang.setUsername("zhang_lab");
        zhang.setPassword("123456");
        zhang.setName("张老师");
        zhang.setRole(User.UserRole.LAB_MANAGER);
        zhang.setCollege(college1);
        zhang.setContactInfo("zhang@university.edu");
        zhang.setIsActive(true);
        zhang.setCreateTime(LocalDateTime.now());
        zhang = userRepository.save(zhang);
        
        User li = new User();
        li.setUsername("li_lab");
        li.setPassword("123456");
        li.setName("李老师");
        li.setRole(User.UserRole.LAB_MANAGER);
        li.setCollege(college3);
        li.setContactInfo("li@university.edu");
        li.setIsActive(true);
        li.setCreateTime(LocalDateTime.now());
        li = userRepository.save(li);
        
        User wang = new User();
        wang.setUsername("wang_lab");
        wang.setPassword("123456");
        wang.setName("王老师");
        wang.setRole(User.UserRole.LAB_MANAGER);
        wang.setCollege(college4);
        wang.setContactInfo("wang@university.edu");
        wang.setIsActive(true);
        wang.setCreateTime(LocalDateTime.now());
        wang = userRepository.save(wang);
        
        // 创建实验室
        Laboratory lab1 = new Laboratory();
        lab1.setName("计算机基础实验室");
        lab1.setCollege(college1);
        lab1 = laboratoryRepository.save(lab1);
        
        Laboratory lab2 = new Laboratory();
        lab2.setName("计算机网络实验室");
        lab2.setCollege(college1);
        lab2 = laboratoryRepository.save(lab2);
        
        Laboratory lab3 = new Laboratory();
        lab3.setName("软件工程实验室");
        lab3.setCollege(college1);
        lab3 = laboratoryRepository.save(lab3);
        
        Laboratory lab4 = new Laboratory();
        lab4.setName("机械制造实验室");
        lab4.setCollege(college2);
        lab4 = laboratoryRepository.save(lab4);
        
        Laboratory lab5 = new Laboratory();
        lab5.setName("机械设计实验室");
        lab5.setCollege(college2);
        lab5 = laboratoryRepository.save(lab5);
        
        Laboratory lab6 = new Laboratory();
        lab6.setName("化学分析实验室");
        lab6.setCollege(college3);
        lab6 = laboratoryRepository.save(lab6);
        
        Laboratory lab7 = new Laboratory();
        lab7.setName("有机化学实验室");
        lab7.setCollege(college3);
        lab7 = laboratoryRepository.save(lab7);
        
        Laboratory lab8 = new Laboratory();
        lab8.setName("物理光学实验室");
        lab8.setCollege(college4);
        lab8 = laboratoryRepository.save(lab8);
        
        Laboratory lab9 = new Laboratory();
        lab9.setName("物理力学实验室");
        lab9.setCollege(college4);
        lab9 = laboratoryRepository.save(lab9);
        
        Laboratory lab10 = new Laboratory();
        lab10.setName("生物分子实验室");
        lab10.setCollege(college5);
        lab10 = laboratoryRepository.save(lab10);
        
        // 分配实验室给管理员
        zhang.setLaboratories(Arrays.asList(lab1, lab2, lab3));
        li.setLaboratories(Arrays.asList(lab6, lab7));
        wang.setLaboratories(Arrays.asList(lab8, lab9));
        
        userRepository.save(zhang);
        userRepository.save(li);
        userRepository.save(wang);
        
        // 创建一些示例安全检查记录
        SafetyCheck check1 = new SafetyCheck();
        check1.setTerm(term2); // 设置当前学期
        check1.setLaboratory(lab1);
        check1.setUser(zhang);
        check1.setCheckDate(LocalDate.of(2023, 10, 15));
        check1.setCheckerName("张老师");
        check1.setWindowStatus(SafetyCheck.CheckStatus.OK);
        check1.setCircuitStatus(SafetyCheck.CheckStatus.OK);
        check1.setFireStatus(SafetyCheck.CheckStatus.OK);
        check1.setEquipmentStatus(SafetyCheck.CheckStatus.OK);
        check1.setItemsStatus(SafetyCheck.CheckStatus.OK);
        check1.setHygieneStatus(SafetyCheck.CheckStatus.OK);
        check1.setOverallStatus(SafetyCheck.OverallStatus.NORMAL);
        check1.setHandleStatus(SafetyCheck.HandleStatus.PENDING);
        check1.setCreateTime(LocalDateTime.now());
        safetyCheckRepository.save(check1);
        
        SafetyCheck check2 = new SafetyCheck();
        check2.setTerm(term2); // 设置当前学期
        check2.setLaboratory(lab6);
        check2.setUser(li);
        check2.setCheckDate(LocalDate.of(2023, 10, 15));
        check2.setCheckerName("李老师");
        check2.setWindowStatus(SafetyCheck.CheckStatus.OK);
        check2.setCircuitStatus(SafetyCheck.CheckStatus.PROBLEM);
        check2.setFireStatus(SafetyCheck.CheckStatus.OK);
        check2.setEquipmentStatus(SafetyCheck.CheckStatus.OK);
        check2.setItemsStatus(SafetyCheck.CheckStatus.OK);
        check2.setHygieneStatus(SafetyCheck.CheckStatus.OK);
        check2.setOverallStatus(SafetyCheck.OverallStatus.ABNORMAL);
        check2.setHandleStatus(SafetyCheck.HandleStatus.HANDLED);
        check2.setRemark("电路线路老化，已安排维修");
        check2.setCreateTime(LocalDateTime.now());
        check2.setHandleTime(LocalDateTime.now());
        safetyCheckRepository.save(check2);
        
        System.out.println("数据初始化完成！");
        System.out.println("测试账号：");
        System.out.println("超级管理员 - 用户名: admin, 密码: 123456");
        System.out.println("实验室管理员 - 用户名: zhang_lab, 密码: 123456");
        System.out.println("实验室管理员 - 用户名: li_lab, 密码: 123456");
        System.out.println("实验室管理员 - 用户名: wang_lab, 密码: 123456");
    }
} 